home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / URL Helper II / Source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-14  |  7.1 KB  |  290 lines  |  [TEXT/MMCC]

  1. /***
  2.  * main.c
  3.  *
  4.  *  Tester routine for the URLHelper component.  This routine will try to exercise all the
  5.  *  various facets of the component.
  6.  *
  7.  ***/
  8. #include "debug_me.h"
  9. #include "URLHelperComponent.h"
  10. #include "URLHelperComponentPrivate.h"
  11. #include <stdio.h>
  12. #include "Exceptions.h"
  13. #include "URLHLibrary.h"
  14.  
  15. char *StrPrint (StringPtr string);
  16. void FullSub (ComponentInstance urlInstace, StringPtr string);
  17. void Lib1Sub (StringPtr string);
  18. void LibAllSub (StringPtr string);
  19.  
  20. /**
  21.  ** Global stuff we need if we are debugging -- this is because we have to supply
  22.  ** all the info about the component when we register.
  23.  **/
  24.  
  25. #ifdef DEBUG_IT
  26. Handle    gURLHelperNameHandle;
  27. Handle    gURLHelperInfoHandle;
  28. #endif
  29.  
  30. // Registration flags
  31. enum
  32. {
  33.     kRegisterLocally = 0,
  34.     kRegisterGlobally
  35. };
  36.  
  37. void main () {
  38.     ComponentInstance        urlInstance;
  39.     short                    numMirrors;
  40.     URLHMirrorListRef        infomacMirrors, archiveMirrors;
  41.     OSErr                    theErr;
  42.  
  43. #ifdef DEBUG_IT
  44.     Component                urlComponentID;
  45.     ComponentDescription    urlDesc;
  46. #endif
  47.  
  48.     /**
  49.      ** Fill in the description of the URLHelper component
  50.      **/
  51.     
  52. #ifdef DEBUG_IT
  53.     urlDesc.componentType = URLHelperComponentType;
  54.     urlDesc.componentSubType = 0L;
  55.     urlDesc.componentManufacturer = 'appl';
  56.     urlDesc.componentFlags = 0L;
  57.     urlDesc.componentFlagsMask = 0L;
  58.  
  59.     /**
  60.      ** Fill in the info and the name for this component that we are going to register
  61.      ** locally.
  62.      **/
  63.  
  64.     {
  65.         Str255    urlInfo = "\pThis component provides simple URLHelper services";
  66.         
  67.         gURLHelperInfoHandle = NewHandle (sizeof(Str255));
  68.         BlockMove ((Ptr) &urlInfo, (Ptr) (*gURLHelperInfoHandle), urlInfo[0]+1);
  69.     }
  70.     
  71.     {
  72.         Str255    urlName = "\pURLHelper Reporter Component";
  73.         
  74.         gURLHelperNameHandle = NewHandle (sizeof(Str255));
  75.         BlockMove ((Ptr) &urlName, (Ptr) (*gURLHelperNameHandle), urlName[0]+1);
  76.     }
  77.  
  78.     /**
  79.      ** Since this puppy is built to do debugging, we want to register the component
  80.      ** locally, as opposed to getting it from the system during boot time.
  81.      **/
  82.     
  83.     urlComponentID = RegisterComponent (&urlDesc, (ComponentRoutine) URLHelperDispatcher,
  84.         kRegisterLocally, gURLHelperNameHandle, gURLHelperInfoHandle, 0L);
  85.  
  86.     /**
  87.      ** Next, see if we can declare an instance of the damm thing
  88.      **/
  89.  
  90.     urlInstance = OpenComponent (urlComponentID);
  91.  
  92. #else
  93.  
  94.     /**
  95.      ** Ok -- so we just need to open the component now
  96.      **/
  97.     
  98.     urlInstance = OpenDefaultComponent (URLHelperComponentType, 0L);
  99. #endif
  100.  
  101.     if (urlInstance == 0)
  102.         return;
  103.  
  104.     /**
  105.      ** If the info-mac string list isn't there, then build it!
  106.      **/
  107.     
  108.     theErr = URLHGetMirrorList (urlInstance, "\pinfo-mac", &infomacMirrors);
  109.     if (theErr == errURLHNoSuchMirrorList) {
  110.         printf ("Creating infomac mirror list...\n");
  111.         theErr = URLHNewMirrorList (urlInstance, "\pinfo-mac", &infomacMirrors);
  112.         nrequire (theErr, FailedToCreateIMMirror);
  113.         
  114.         theErr = URLHAddMirror (urlInstance, infomacMirrors,
  115.             "\p^0://sumex-aim.stanford.edu/info-mac/^1", kURLHMNoUse);
  116.         nrequire (theErr, FailedToAdd1);
  117.         
  118.         theErr = URLHAddMirror (urlInstance, infomacMirrors,
  119.             "\p^0://mac.archive.edu/pub/info-mac/^1", kURLHMDefault);
  120.         nrequire (theErr, FailedToAdd2);
  121.         
  122.         theErr = URLHAddMirror (urlInstance, infomacMirrors,
  123.             "\p^0://mit.mit.edu/pub/mirrors/im/^1", kURLHMDefault);
  124.         nrequire (theErr, FailedToAdd3);
  125.  
  126.         printf ("Creating mac.archive mirror list...\n");
  127.         theErr = URLHNewMirrorList (urlInstance, "\pmac.archive", &archiveMirrors);
  128.         nrequire (theErr, FailedToCreateArcMirror);
  129.         
  130.         theErr = URLHAddMirror (urlInstance, archiveMirrors,
  131.             "\p^0://mac.archive.umich.edu/pub/^1", kURLHMDefault);
  132.         nrequire (theErr, FailedToAdd4);
  133.         
  134.         theErr = URLHAddMirror (urlInstance, archiveMirrors,
  135.             "\p^0://wustl.some.place.edu/pub/umich/^1", kURLHMDefault);
  136.         nrequire (theErr, FailedToAdd5);
  137.         
  138.         theErr = URLHAddMirror (urlInstance, archiveMirrors,
  139.             "\p^0://mit.mit.edu/pub/mirrors/marc/^1", kURLHMDefault);
  140.         nrequire (theErr, FailedToAdd6);
  141.     
  142.     }
  143.     nrequire (theErr, FailedToDoMirrorInit);
  144.  
  145.     /**
  146.      ** See how many of these things are around...
  147.      **/
  148.     
  149.     theErr = URLHNumberOfMirrors (urlInstance, infomacMirrors, &numMirrors);
  150.     nrequire (theErr, FailedToCountMirrors);
  151.     printf ("Number of mirrors in im: %d\n", numMirrors);
  152.  
  153.     theErr = URLHNumberOfMirrors (urlInstance, archiveMirrors, &numMirrors);
  154.     nrequire (theErr, FailedToCountMirrors1);
  155.     printf ("Number of mirrors in arc: %d\n", numMirrors);
  156.  
  157.     /**
  158.      ** Now, given a random URL, loop over all the mirrors... :)
  159.      **/
  160.  
  161.     FullSub (urlInstance, 
  162.         "\pftp://sumex-aim.stanford.edu/info-mac/grf/util/mac-graphics.hqx");
  163.     FullSub (urlInstance,
  164.         "\pgopher://sumex-aim.stanford.edu/info-mac/per/im/infomacv12-044.txt");
  165.     FullSub (urlInstance,
  166.         "\pgopher://mac.archive.umich.edu/pub/system/updates/long/dir/help.txt");
  167.  
  168.     URLHLibUse (urlInstance);
  169.  
  170.     Lib1Sub("\pftp://sumex-aim.stanford.edu/info-mac/grf/util/mac-graphics.hqx");
  171.     Lib1Sub("\pgopher://sumex-aim.stanford.edu/info-mac/per/im/infomacv12-044.txt");
  172.     Lib1Sub("\pgopher://mac.archive.umich.edu/pub/system/updates/long/dir/help.txt");
  173.     LibAllSub("\pftp://sumex-aim.stanford.edu/info-mac/grf/util/mac-graphics.hqx");
  174.     LibAllSub("\pgopher://sumex-aim.stanford.edu/info-mac/per/im/infomacv12-044.txt");
  175.     LibAllSub("\pgopher://mac.archive.umich.edu/pub/system/updates/long/dir/help.txt");
  176.  
  177.     /**
  178.      ** We are done!
  179.      **/
  180.      
  181.      CloseComponent (urlInstance);
  182.  
  183.     return;
  184.  
  185. FailedToFindIMList:
  186.  
  187. FailedToCountMirrors:
  188. FailedToCountMirrors1:
  189. FailedToAdd1:
  190. FailedToAdd2:
  191. FailedToAdd3:
  192. FailedToCreateIMMirror:
  193. FailedToAdd4:
  194. FailedToAdd5:
  195. FailedToAdd6:
  196. FailedToCreateArcMirror:
  197. FailedToDoMirrorInit:
  198.  
  199.     CloseComponent (urlInstance);
  200.     printf ("Error: %d\n", theErr);
  201.  
  202.     return;
  203.  
  204. }
  205.  
  206. /**
  207.  * StrPrint
  208.  *
  209.  *  Dump a string to printf...
  210.  *
  211.  **/
  212. char * StrPrint (StringPtr str)
  213. {
  214.  
  215.     static char    string[256];
  216.     
  217.     BlockMove (str+1, string, str[0]);
  218.     string[str[0]] = '\0';
  219.     return string;
  220. }
  221.  
  222. /**
  223.  * FullSub
  224.  *
  225.  *  Given a string, do the complete parse by accessing the URL Helper compontent
  226.  *  directly.
  227.  *
  228.  **/
  229. void FullSub (ComponentInstance urlInstance, StringPtr string)
  230. {
  231.     OSErr            theErr;
  232.     Str255            newURL;
  233.     short            index;
  234.     URLHParseRef    parseRef;
  235.  
  236.     printf ("Full Test on: '%s'\n", StrPrint(string));
  237.     
  238.     /**
  239.      ** Now, given a random URL, loop over all the mirrors... :)
  240.      **/
  241.  
  242.     theErr = URLHNewParseState (urlInstance, string, &parseRef);
  243.     if (theErr != noErr) {
  244.         printf ("Error parsing state: %d\n", theErr);
  245.         return;
  246.     }
  247.  
  248.     theErr = 0;
  249.     index = 1;
  250.     while (theErr == noErr) {
  251.         theErr = URLHGetMirrorRep (urlInstance, parseRef, newURL, index);
  252.         if (theErr == noErr)
  253.             printf (" -> '%s'\n", StrPrint(newURL));
  254.         index++;
  255.     }
  256.  
  257.     URLHDisposeParseState (urlInstance, parseRef);
  258.  
  259.     return;
  260. }
  261. /**
  262.  * Lib1Sub
  263.  *
  264.  *  Use the library to get the primary miirror back
  265.  **/
  266. void Lib1Sub (StringPtr string)
  267. {
  268.     Str255        mirror;
  269.  
  270.     printf ("Getting primary mirror for '%s'\n", StrPrint(string));
  271.     URLHLibGetFirstMirror (string, mirror);
  272.     printf ("And it is: '%s'\n", StrPrint(mirror));
  273. }
  274.  
  275. void LibAllSub (StringPtr string)
  276. {
  277.     short            index;
  278.     URLHParseRef    parseRef;
  279.     Str255            mirror;
  280.  
  281.     printf ("Getting all mirrors for '%s'\n", StrPrint(string));
  282.     parseRef = URLHLibNewParseState (string);
  283.     index = 1;
  284.     while (URLHLibGetIndexedMirror (parseRef, index, mirror)) {
  285.         printf ("-> '%s'\n", StrPrint(mirror));
  286.         index++;
  287.     }
  288.     URLHLibDisposeParseState (parseRef);
  289. }
  290.